export default {
    data() {
        return {
            SRC: '',
            /*当前页显示数量*/
            pageSize: 10,
            /*每页显示的数量*/
            pageSizes: [10, 20, 50, 100],
            /*dataTotal表数量*/
            dataTotal: 0,
            /*分页器*/
            currentPage: 1,
            // form输入框的胡宽度
            formLabelWidth: '95px',
            /*initWidth 的宽*/
            initWidth: '100%',
            xlsxTitle: '',
            xlsxTitleQ: '',
            ruleForm: {
                region: '',
                id: '',
                affiliation: '',
                actName: '',
                channelName: '',
                agent: '',
                platform: '',
                paytype: '',
                advPosition: ''
            },
            /*表格数据*/
            tableHead: [{
                    "prop": "id",
                    "label": "广告ID"
                },
                {
                    "prop": "gameName",
                    "label": "游戏名称"
                },
                {
                    "prop": "gameId",
                    "label": "游戏ID"
                },
                {
                    "prop": "affiliation",
                    "label": "归属"
                },
                {
                    "prop": "actName",
                    "label": "活动名称"
                },
                {
                    "prop": "channelName",
                    "label": "渠道名称"
                },
                {
                    "prop": "agent",
                    "label": "代理公司"
                },
                {
                    "prop": "platform",
                    "label": "平台"
                },
                {
                    "prop": "paytype",
                    "label": "结算方式"
                },
                {
                    "prop": "advPosition",
                    "label": "广告位置"
                }
            ],
            // 游戏列表数据
            options5: [],
            // 表格数据
            tableData: [],
            // 验证ruleForm
            rules: {
                region: [
                    { required: true, message: '请选择游戏名称' },
                ],
                enuNum: [
                    { required: true, message: 'ID不能为空' },
                    { type: 'number', message: 'ID必须为数字值' }
                ],
                enuKey: [
                    { min: 2, max: 100, message: '长度在 2 到 100个字符', trigger: 'blur' }
                ],
                enuValue: [
                    { min: 2, max: 100, message: '长度在 2 到 100个字符', trigger: 'blur' }
                ],
            },
            // 修改弹窗出现
            revampShow: false,
            // 修改弹窗数据
            revampForm: {
                region: '',
                id: '',
                affiliation: '',
                actName: '',
                channelName: '',
                agent: '',
                platform: '',
                paytype: '',
                advPosition: ''
            },
            // 验证 弹窗数据
            revamp: {
                region: "",
                adNum: 0,
                adKey: '',
                adValue: '',
                adDesc: ''
            },
            // 上传文件显示隐藏
            uploadShow: false,
            // 下载excel文件
            advCostData: [],
        }
    },
    methods: {
        // 通用 成功 与 失败
        successFun(data) {
            this.$message({
                message: data,
                type: 'success'
            });
        },
        errorFun(data) {
            this.$message.error(data);
        },
        /*设置初始值*/
        _init() {
            window.addEventListener('resize', function() {
                /*修改表格的宽度*/
                this.initWidth = document.body.clientWidth - 220 + "px";
            }.bind(this))
        },
        // 控制每页的数量
        handleSizeChange(val) {
            this.pageSize = val;
            this.getPageData(`curPage=${this.currentPage}&pageSize=${this.pageSize}`);
        },
        // 页数
        handleCurrentChange(val) {
            this.currentPage = val;
            this.getPageData(`curPage=${this.currentPage}&pageSize=${this.pageSize}`);
        },
        // 表格下载
        excel(sheet) {
            var that = this;
            that.downloadExl(that.tableHead, that.tableData, sheet, sheet);
        },
        // 获取数据 获取工程List 以及页面展示数据
        getProjectData() {
            this.options5 = [];
            this.$http.post(`${this.SRC}project/list.do`).then(
                function(res) {
                    var data = res.body;
                    console.log(data)
                    if (data["code"] === 0) {
                        for (var i = 0; i < data['msg'].length; i++) {
                            var obj = {
                                label: data["msg"][i]['projectName'],
                                value: data["msg"][i]['id']
                            }
                            this.options5.push(obj);
                        }
                    } else {
                        this.errorFun(data['msg'])
                    }
                }
            )
        },
        upload() {
            this.getPageData(`curPage=${this.currentPage}&pageSize=${this.pageSize}`);
        },
        //获取页面数据
        getPageData(str) {
            var that = this;
            var p = new Promise(
                function(resovle, reject) {
                    that.$http.post(`${that.SRC}advsysconfig/list.do?${str}`).then(
                        function(res) {
                            resovle(res);
                        }
                    )
                }
            )
            p.then(function(value) {
                that.tableData = [];
                var data = value.body;
                if (data['code'] === 0) {
                    that.tableData = data.msg.data;
                    that.dataTotal = data.msg.totalSize;
                } else {
                    that.errorFun(data['msg'])
                }
            })
        },
        // 上传
        uploadForm() {
            this.uploadShow = true;
        },
        // 修改用户
        userEdit(index, row) {
            this.revampForm = row;
            this.revampForm.password = '';
            this.userShow = true;
        },
        // 编辑权限
        edit(index, row) {
            this.revampForm.id = row.id;
            this.revampForm.affiliation = row.affiliation;
            this.revampForm.actName = row.actName;
            this.revampForm.channelName = row.channelName;
            this.revampForm.agent = row.agent;
            this.revampForm.platform = row.platform;
            this.revampForm.paytype = row.paytype;
            this.revampForm.advPosition = row.advPosition;
            this.revampForm.region = row.gameId;
            this.revampShow = true;
        },
        // 删除用户
        handleDelete(index, row) {
            var obj = {
                id: row.id,
            }
            this.$http.post(`${this.SRC}advsysconfig/delete.do`, obj, { emulateJSON: true }).then(
                function(res) {
                    if (res.body['code'] === 0) {
                        this.successFun(res.body["msg"]);
                        this.getPageData(`curPage=${this.currentPage}&pageSize=${this.pageSize}`);
                    } else {
                        this.errorFun(res.body["msg"])
                    }
                }
            )
        },
        // 添加系统配置
        //http://10.236.44.188:8080/advsysconfig/add.do?id=1&affiliation=guishu111&actName=game1111&channelName=channel2&agent=aa&platform=pp&paytype=pay1&advPosition=posi&gameId=1
        addMessage() {
            var obj = {
                id: this.ruleForm.id,
                affiliation: this.ruleForm.affiliation,
                actName: this.ruleForm.actName,
                channelName: this.ruleForm.channelName,
                agent: this.ruleForm.agent,
                platform: this.ruleForm.platform,
                paytype: this.ruleForm.paytype,
                advPosition: this.ruleForm.advPosition,
                gameId: this.ruleForm.region,
            }
            this.$http.post(`${this.SRC}advsysconfig/add.do`, obj, { emulateJSON: true }).then(
                function(res) {
                    if (res.body['code'] === 0) {
                        this.successFun(res.body["msg"]);
                        this.getPageData(`curPage=${this.currentPage}&pageSize=${this.pageSize}`);
                    } else {
                        this.errorFun(res.body["msg"])
                    }
                }
            )
        },
        // 添加提交
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.addMessage()
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        // 重置
        resetForm(formName) {
            for (var i in this.formName) {
                this.formName[i] = '';
            }
            this.region = '';
            this.$refs[formName].resetFields();
        },
        revampReset(formName) {
            for (var i in this.formName) {
                this.formName[i] = '';
            }
            this.region = '';
            this.$refs[formName].resetFields();
        },
        // 修改信息内容
        revampMsg(formName) {
            var obj = {
                id: this.revampForm.id,
                affiliation: this.revampForm.affiliation,
                actName: this.revampForm.actName,
                channelName: this.revampForm.channelName,
                agent: this.revampForm.agent,
                platform: this.revampForm.platform,
                paytype: this.revampForm.paytype,
                advPosition: this.revampForm.advPosition,
                gameId: this.revampForm.region,
            };
            this.$http.post(`${this.SRC}advsysconfig/modify.do`, obj, { emulateJSON: true }).then(
                function(res) {
                    if (res.body['code'] === 0) {
                        this.successFun(res.body["msg"]);
                        this.getPageData(`curPage=${this.currentPage}&pageSize=${this.pageSize}`);
                    } else {
                        this.errorFun(res.body["msg"])
                    }
                }
            )
        },
        revamps(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.revampMsg();
                    console.log(1)
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        revampReset(formName) {
            for (var i in this.revampForm) {
                this.revampForm[i] = '';
            }
            this.revampRegion = '';
            this.$refs[formName].resetFields();
        },
        //搜索提交
        findForm() {
            var obj = {
                id: this.ruleForm.id,
                affiliation: this.ruleForm.affiliation,
                actName: this.ruleForm.actName,
                channelName: this.ruleForm.channelName,
                agent: this.ruleForm.agent,
                platform: this.ruleForm.platform,
                paytype: this.ruleForm.paytype,
                advPosition: this.ruleForm.advPosition,
                gameId: this.ruleForm.region,
            }
            var str = '';
            for (var i in obj) {
                if (obj[i] !== "" && typeof(obj[i]) !== 'object') {
                    str += `${i}=${obj[i]}&`
                    console.log(str)
                }
            }
            str = str.substring(0, str.length - 1);
            this.getPageData(`curPage=${this.currentPage}&pageSize=${this.pageSize}&${str}`);
        },
        revampReset(formName) {
            this.$refs[formName].resetFields();
        }
    },
    mounted() {
        this._init();
        this.SRC = this.$store.getters.src3;
        // 获取工程的名字
        this.getProjectData();
        //获取页面数据
        this.getPageData(`curPage=${this.currentPage}&pageSize=${this.pageSize}`);

    }
}